#ifndef __EEPROM_H
#define __EEPROM_H

#include "at32f421.h"
#include "at32f421_board.h"
#include "i2c_application.h"

#define EEPROM_ADDR 										 0x50 << 1

#define I2C_TIMEOUT                      0xFFFFFFFF

#define I2Cx_SPEED                       100000
#define I2Cx_ADDRESS                     0xA0

#define I2Cx_PORT                        I2C2
#define I2Cx_CLK                         CRM_I2C2_PERIPH_CLOCK

#define I2Cx_SCL_PIN                     GPIO_PINS_6
#define I2Cx_SCL_PIN_SOURCE              GPIO_PINS_SOURCE6
#define I2Cx_SCL_PIN_MUX_NUM             GPIO_MUX_0
#define I2Cx_SCL_GPIO_PORT               GPIOF
#define I2Cx_SCL_GPIO_CLK                CRM_GPIOF_PERIPH_CLOCK

#define I2Cx_SDA_PIN                     GPIO_PINS_7
#define I2Cx_SDA_PIN_SOURCE              GPIO_PINS_SOURCE7
#define I2Cx_SDA_PIN_MUX_NUM             GPIO_MUX_0
#define I2Cx_SDA_GPIO_PORT               GPIOF
#define I2Cx_SDA_GPIO_CLK                CRM_GPIOF_PERIPH_CLOCK

#define I2Cx_DMA_CLK                     CRM_DMA1_PERIPH_CLOCK
#define I2Cx_DMA_TX_CHANNEL              DMA1_CHANNEL4
#define I2Cx_DMA_TX_IRQn                 DMA1_Channel5_4_IRQn

#define I2Cx_DMA_RX_CHANNEL              DMA1_CHANNEL5
#define I2Cx_DMA_RX_IRQn                 DMA1_Channel5_4_IRQn

#define I2Cx_EVT_IRQn                    I2C2_EVT_IRQn
#define I2Cx_ERR_IRQn                    I2C2_ERR_IRQn

void eeprom_i2c_init(void);
void eeprom_read_page(uint8_t addr, uint8_t *rx_buf);
void eeprom_write_page(uint8_t *data);
void eeprom_write_int16(uint8_t addr, int16_t value);
void eeprom_read_int16(uint8_t addr, int16_t *value);

#endif